import { socket } from './server';

export function socketio() {
  let socketver = socket();
  function createWsServer() {
    console.log(socketver);
  }
  function getMixinWs(customSocketFunction?: Function) {
    console.log('这里');

    socketver.onopen = () => {
      console.log('WebSocket连接已建立');
      createWsServer();
    };
    socketver.onmessage = (event) => {
      if (event && event.data !== '连接成功') {
        const message = JSON.parse(event.data);
        if (customSocketFunction) {
          customSocketFunction(message);
        }
      }
    };
    socketver.onerror = (error) => {
      console.error('WebSocket error:', error);
      // 处理错误
    };
    socketver.onclose = () => {
      console.log('WebSocket连接已关闭');
      socketver = socket();
      getMixinWs();
      // 处理连接关闭
    };
  }
  return {
    socketver,
    getMixinWs,
  };
}
